package core.Resource
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;

	public class MapPack extends BasePack
	{
		public var data:Array;
		
		private var _loader:Loader;
		
		public function MapPack()
		{
			data = [];
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, completeHandler);
		}
		
		public override function load(packName:String,url:String):void
		{
			name = packName;
			var srcPackageContext:LoaderContext = new LoaderContext();
			srcPackageContext.applicationDomain= new ApplicationDomain();
			_loader.load(new URLRequest(url),srcPackageContext);	
			state = 0;
		}
		
		
		private function completeHandler(e:Event):void
		{
			if (e.type == Event.COMPLETE)
			{
				var blocks:Array=e.target.loader.content.tempA;
				//trace(tArr[0]);
				for(var i:int = 0;i<blocks.length;i++)
				{
					data[getNameByIndex(name,i)] = blocks[i];
				}
				_loader.unloadAndStop();
				state = 1;
				this.dispatchEvent(new Event(Event.COMPLETE));
			}
			else
			{
				throw new Error("TGP:Animation pack load error.Pack name:"+name);
			}
		}
		
		public function getNameByIndex(ori:String,i:int):String
		{
			var spliteName:Array = ori.split("_");
			var x:int = int(spliteName[0]);
			var y:int = int(spliteName[1]);
			if(i == 0)
				return ori;
			else if(i == 1)
				return x.toString()+"_"+(y+1).toString();
			else if(i == 2)
				return (x+1).toString()+"_"+y.toString();
			else if(i == 3)
				return (x+1).toString()+"_"+(y+1).toString();
			else return "";

		}
		
		public override function reset():void
		{
			name = "";
			state = -1;
			data = [];
			_loader.unloadAndStop();
		}
		
		public override function dispose():void
		{
			reset();
			if(_loader!=null)
			{
				_loader.removeEventListener(Event.COMPLETE,completeHandler);
				_loader.removeEventListener(IOErrorEvent.IO_ERROR, completeHandler);
				_loader = null
			}
		}
	}
}